﻿namespace EyeOpen.Security.Server.BootStrapper
{
    using EyeOpen.Data;
    using EyeOpen.Data.Nhibernate;
    using EyeOpen.Data.Nibernate.Mapping;
    using EyeOpen.Dependency;

    internal class SecurityContainerConfigurator : IContainerConfigurator
    {
        public const string SecurityConfigurationName = "Security.Sql";

        public void Configure(IDependencyContainer container)
        {
            RegisterSecurityDataContext(container);
        }

        private static void RegisterSecurityDataContext(IDependencyContainer container)
        {
            var mappings = 
                new[]
                    {
                        typeof(UniqueEntityMap<>).Assembly, 
                        typeof(CredentialMap).Assembly
                    };

            var securitySqlFluentConfiguration = new FluentConfigurationHelper().CreateFluentConfiguration("SecurityConnection", mappings);
            var sessionFactory = securitySqlFluentConfiguration.BuildSessionFactory();
            securitySqlFluentConfiguration.BuildConfiguration();

            container.RegisterInstance(SecurityConfigurationName, sessionFactory);

            var securityDataBase = new NhibernateDataBase(securitySqlFluentConfiguration);
            container.RegisterInstance<IDatabase>(SecurityConfigurationName, securityDataBase);
        }
    }
}